// var i;
// var $bundleConfig = require("./BundleConfig");
// var $eventManager = require("./EventManager");
// var $resKeeper = require("./ResKeeper");
// var $commonUtils = require("./CommonUtils");
// var $evenType = require("./EvenType");
// var $excel = require("./Excel");
// var $gameGlobalVariable = require("./GameGlobalVariable");
// var _ = cc._decorator;
// var y = _.ccclass;
// var m = _.property;
// var v = _.inspector;
// var b = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.scroll = null;
//         t.tipTongji = null;
//         t.maxvalue = 1e4;
//         t.ids = 0;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype._show = function (e) {
//         var t = [];
//         for (var o = 1; o < arguments.length; o++) {
//             t[o - 1] = arguments[o];
//         }
//     };
//     t.prototype.onEnable = function () {
//         $eventManager.EventMgr.addEventListener($evenType.EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onShangHaiTong, this);
//         this.hideTongji();
//     };
//     t.prototype.onDisable = function () {
//         $eventManager.EventMgr.rmEventListener($evenType.EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onShangHaiTong, this);
//     };
//     t.prototype.showTongJi = function () {
//         if (this.tipTongji.active) {
//             this.hideTongji();
//         } else {
//             this.tipTongji.active = !0;
//             this.tipTongji.scale = 0;
//             cc.tween(this.tipTongji)
//                 .to(0.1, {
//                     scale: 1
//                 })
//                 .start();
//             this.onShangHaiTong();
//         }
//     };
//     t.prototype.hideTongji = function () {
//         var e = this;
//         if (this.tipTongji.active) {
//             cc.tween(this.tipTongji)
//                 .to(0.1, {
//                     scale: 0
//                 })
//                 .call(function () {
//                     e.tipTongji.active = !1;
//                 })
//                 .start();
//         }
//     };
//     t.prototype.onShangHaiTong = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var o;
//             var n;
//             var i;
//             var a;
//             var r;
//             var c;
//             var u;
//             var d;
//             var h;
//             var _;
//             var y;
//             var m;
//             var v;
//             var b;
//             var w;
//             var I;
//             var M;
//             return __generator(this, function (s) {
//                 switch (s.label) {
//                     case 0:
//                         for (
//                             e = this.scroll.content.children,
//                                 t = (t = Object.keys($gameGlobalVariable.GameGlobalVariable.nowwShangHai)).sort(
//                                     function (e, t) {
//                                         if (
//                                             $gameGlobalVariable.GameGlobalVariable.nowwShangHai[e] >
//                                             $gameGlobalVariable.GameGlobalVariable.nowwShangHai[t]
//                                         ) {
//                                             return -1;
//                                         } else {
//                                             return 1;
//                                         }
//                                     }
//                                 ),
//                                 this.ids = 0,
//                                 o = 0,
//                                 a = 0;
//                             a < t.length;
//                             a++
//                         ) {
//                             n = t[a];
//                             i = $gameGlobalVariable.GameGlobalVariable.nowwShangHai[n];
//                             o += i;
//                         }
//                         a = 0;
//                         s.label = 1;
//                     case 1:
//                         if (a < 5) {
//                             return (
//                                 (r = t[a]),
//                                 (c = e[a]),
//                                 r
//                                     ? (c || ((c = cc.instantiate(e[0])), this.scroll.content.addChild(c)),
//                                       (c.active = !0),
//                                       (u = c.children[0].getComponent(cc.Sprite)),
//                                       (d = $excel.Excel.shuiguo(1e3 + Number(r))),
//                                       [
//                                           4,
//                                           this._loadRes(
//                                               $bundleConfig.BundleNames.Game,
//                                               "res/tongji" + d.quality,
//                                               cc.SpriteFrame
//                                           )
//                                       ])
//                                     : [3, 6]
//                             );
//                         } else {
//                             return [3, 8];
//                         }
//                     case 2:
//                         h = s.sent();
//                         u.spriteFrame = h;
//                         _ = c.children[2].getComponent(cc.Sprite);
//                         return [
//                             4,
//                             this._loadRes($bundleConfig.BundleNames.Game, "res/tongjipro" + (a + 1), cc.SpriteFrame)
//                         ];
//                     case 3:
//                         y = s.sent();
//                         _.spriteFrame = y;
//                         return (m = c.children[1].getComponent(cc.Sprite)).name == r
//                             ? [3, 5]
//                             : [
//                                   4,
//                                   this._loadRes(
//                                       $bundleConfig.BundleNames.Common,
//                                       "res/player/player" + r + "_1",
//                                       cc.SpriteFrame
//                                   )
//                               ];
//                     case 4:
//                         v = s.sent();
//                         m.spriteFrame = v;
//                         m.node.name = r;
//                         $commonUtils.default.LimitSpriteMaxSize(m, 60);
//                         s.label = 5;
//                     case 5:
//                         b = c.children[3].getComponent(cc.Label);
//                         w = $gameGlobalVariable.GameGlobalVariable.nowwShangHai[r];
//                         w = this.formatNumberWithChineseUnit(w);
//                         b.string = w;
//                         I = c.children[2];
//                         M = $gameGlobalVariable.GameGlobalVariable.nowwShangHai[r] / o;
//                         I.width = 250 * M;
//                         return [3, 7];
//                     case 6:
//                         if (c) {
//                             c.active = !1;
//                         }
//                         s.label = 7;
//                     case 7:
//                         a++;
//                         return [3, 1];
//                     case 8:
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.formatNumberWithChineseUnit = function (e) {
//         if (e >= 1e8) {
//             return (e / 1e8).toFixed(1) + "E";
//         } else {
//             return e >= 1e4 ? (e / 1e4).toFixed(1) + "W" : (e + "").split(".")[0];
//         }
//     };
//     __decorate([m(cc.ScrollView)], t.prototype, "scroll", void 0);
//     __decorate([m(cc.Node)], t.prototype, "tipTongji", void 0);
//     return __decorate([y], t);
// })($resKeeper.default);
// exports.default = b;


// import { BundleNames } from "./BundleConfig";
// import { EventMgrInstance } from "./EventManager";
// import ResKeeper from "./ResKeeper";
// import CommonUtils from "./CommonUtils";
// import { EVENT_TYPE } from "./EvenType";
// import { Excel } from "./Excel";
// import { GameGlobalVariable } from "./GameGlobalVariable";

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名: ShanghaiStatistics
//  */
// @ccclass
// class UI_AttackTongJi extends ResKeeper {
//     @property(cc.ScrollView)
//     public scroll: cc.ScrollView = null;

//     @property(cc.Node)
//     public tipTongji: cc.Node = null;

//     public maxvalue: number = 10000;
//     public ids: number = 0;

//     /**
//      * 建议方法名: show
//      * @param e 
//      * @param args 
//      */
//     private _show(e: any, ...args: any[]): void {
//         // 该方法目前没有实现任何逻辑
//     }

//     /**
//      * 当组件启用时调用
//      * 建议方法名: onComponentEnable
//      */
//     public onEnable(): void {
//         EventMgrInstance.addEventListener(EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onShangHaiTong, this);
//         this.hideTongji();
//     }

//     /**
//      * 当组件禁用时调用
//      * 建议方法名: onComponentDisable
//      */
//     public onDisable(): void {
//         EventMgrInstance.rmEventListener(EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onShangHaiTong, this);
//     }

//     /**
//      * 显示统计信息
//      * 建议方法名: displayStatistics
//      */
//     public showTongJi(): void {
//         if (this.tipTongji.active) {
//             this.hideTongji();
//         } else {
//             this.tipTongji.active = true;
//             this.tipTongji.scale = 0;
//             cc.tween(this.tipTongji)
//                 .to(0.1, { scale: 1 })
//                 .start();
//             this.onShangHaiTong();
//         }
//     }

//     /**
//      * 隐藏统计信息
//      * 建议方法名: hideStatistics
//      */
//     public hideTongji(): void {
//         if (this.tipTongji.active) {
//             cc.tween(this.tipTongji)
//                 .to(0.1, { scale: 0 })
//                 .call(() => {
//                     this.tipTongji.active = false;
//                 })
//                 .start();
//         }
//     }

//     /**
//      * 处理上海统计事件
//      * 建议方法名: handleShanghaiStatistics
//      */
//     public async onShangHaiTong(): Promise<void> {
//         const children = this.scroll.content.children;
//         const sortedKeys = Object.keys(GameGlobalVariable.nowwShangHai).sort((a, b) => {
//             return GameGlobalVariable.nowwShangHai[b] - GameGlobalVariable.nowwShangHai[a];
//         });

//         this.ids = 0;
//         let totalDamage = 0;

//         for (const key of sortedKeys) {
//             totalDamage += GameGlobalVariable.nowwShangHai[key];
//         }

//         for (let i = 0; i < 5; i++) {
//             const key = sortedKeys[i];
//             let child = children[i];

//             if (key) {
//                 if (!child) {
//                     child = cc.instantiate(children[0]);
//                     this.scroll.content.addChild(child);
//                 }
//                 child.active = true;

//                 const spriteComponent = child.children[0].getComponent(cc.Sprite);
//                 const excelData = Excel.shuiguo(1000 + Number(key));
//                 const spriteFrame = await this._loadRes(BundleNames.Game, `res/tongji${excelData.quality}`, cc.SpriteFrame);
//                 spriteComponent.spriteFrame = spriteFrame;

//                 const progressBarSprite = child.children[2].getComponent(cc.Sprite);
//                 const progressBarFrame = await this._loadRes(BundleNames.Game, `res/tongjipro${i + 1}`, cc.SpriteFrame);
//                 progressBarSprite.spriteFrame = progressBarFrame;

//                 const playerSprite = child.children[1].getComponent(cc.Sprite);
//                 if (playerSprite.name !== key) {
//                     const playerFrame = await this._loadRes(BundleNames.Common, `res/player/player${key}_1`, cc.SpriteFrame);
//                     playerSprite.spriteFrame = playerFrame;
//                     playerSprite.node.name = key;
//                     CommonUtils.LimitSpriteMaxSize(playerSprite, 60);
//                 }

//                 const labelComponent = child.children[3].getComponent(cc.Label);
//                 let damageNum = GameGlobalVariable.nowwShangHai[key];
//                 let damageStr = this.formatNumberWithChineseUnit(damageNum);
//                 labelComponent.string = damageStr;

//                 const progressBarNode = child.children[2];
//                 const progressBarWidth = (250 * GameGlobalVariable.nowwShangHai[key]) / totalDamage;
//                 progressBarNode.width = progressBarWidth;
//             } else if (child) {
//                 child.active = false;
//             }
//         }
//     }

//     /**
//      * 格式化数字为带有中文单位的字符串
//      * 建议方法名: formatNumberWithChineseUnit
//      * @param num 
//      * @returns 
//      */
//     public formatNumberWithChineseUnit(num: number): string {
//         if (num >= 1e8) {
//             return (num / 1e8).toFixed(1) + "E";
//         } else if (num >= 1e4) {
//             return (num / 1e4).toFixed(1) + "W";
//         } else {
//             return num.toString().split(".")[0];
//         }
//     }
// }

// export default UI_AttackTongJi;

import { BundleNames } from "./BundleConfig";
import { EventMgrInstance } from "./EventManager";
import ResKeeper from "./ResKeeper";
import CommonUtils from "./CommonUtils";
import { EVENT_TYPE } from "./EvenType";
import { Excel } from "./Excel";
import { GameGlobalVariable } from "./GameGlobalVariable";

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名: ShanghaiStatistics
 */
@ccclass
class ViewAttackTongJi extends ResKeeper {
    @property(cc.ScrollView)
    public scroll: cc.ScrollView = null;

    @property(cc.Node)
    public tipTongji: cc.Node = null;

    public maxvalue: number = 10000;
    public ids: number = 0;

    /**
     * 建议方法名: show
     * @param e 
     * @param args 
     */
    private _show(event: any, ...params: any[]): void {
        // 该方法目前没有实现任何逻辑
    }

    /**
     * 当组件启用时调用
     * 建议方法名: onComponentEnable
     */
    public onEnable(): void {
        EventMgrInstance.addListener(EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onDamageTongJi, this);
        this.hideStatistics();
    }

    /**
     * 当组件禁用时调用
     * 建议方法名: onComponentDisable
     */
    public onDisable(): void {
        EventMgrInstance.removeListener(EVENT_TYPE.GAME_SHANGHAI_TONGJI, this.onDamageTongJi, this);
    }

    /**
     * 显示统计信息
     * 建议方法名: displayStatistics
     */
    // public showTongJi(): void {
    public displayStatistics(): void {
        if (this.tipTongji.active) {
            this.hideStatistics();
        } else {
            this.tipTongji.active = true;
            this.tipTongji.scale = 0;
            cc.tween(this.tipTongji)
                .to(0.1, { scale: 1 })
                .start();
            this.onDamageTongJi();
        }
    }

    /**
     * 隐藏统计信息
     * 建议方法名: hideStatistics
     */
    // public hideTongji(): void {
    public hideStatistics(): void {
        if (this.tipTongji.active) {
            cc.tween(this.tipTongji)
                .to(0.1, { scale: 0 })
                .call(() => {
                    this.tipTongji.active = false;
                })
                .start();
        }
    }

    /**
     * 处理上海统计事件
     * 建议方法名: handleShanghaiStatistics
     */
    public async onDamageTongJi(): Promise<void> {
        const { children } = this.scroll.content;
        const sortedKeys = Object.keys(GameGlobalVariable.nowwShangHai).sort((a, b) => {
            return GameGlobalVariable.nowwShangHai[b] - GameGlobalVariable.nowwShangHai[a];
        });

        this.ids = 0;
        let totalDamage = 0;

        for (const key of sortedKeys) {
            totalDamage += GameGlobalVariable.nowwShangHai[key];
        }

        for (let i = 0; i < 5; i++) {
            const key = sortedKeys[i];
            let childNode = children[i];

            if (key) {
                if (!childNode) {
                    childNode = cc.instantiate(children[0]);
                    this.scroll.content.addChild(childNode);
                }
                childNode.active = true;

                const spriteComponent = childNode.children[0].getComponent(cc.Sprite);
                const excelData = Excel.shuiguo(1000 + Number(key));
                const spriteFrame = await this._loadRes(BundleNames.Game, `res/tongji${excelData.quality}`, cc.SpriteFrame);
                spriteComponent.spriteFrame = spriteFrame;

                const progressBarSprite = childNode.children[2].getComponent(cc.Sprite);
                const progressBarFrame = await this._loadRes(BundleNames.Game, `res/tongjipro${i + 1}`, cc.SpriteFrame);
                progressBarSprite.spriteFrame = progressBarFrame;

                const playerSprite = childNode.children[1].getComponent(cc.Sprite);
                if (playerSprite.name !== key) {
                    const playerFrame = await this._loadRes(BundleNames.Common, `res/player/player${key}_1`, cc.SpriteFrame);
                    playerSprite.spriteFrame = playerFrame;
                    playerSprite.node.name = key;
                    CommonUtils.limitSpriteSize(playerSprite, 60);
                }

                const labelComponent = childNode.children[3].getComponent(cc.Label);
                let damageNum = GameGlobalVariable.nowwShangHai[key];
                let damageStr = this.formatNumberWithChineseUnit(damageNum);
                labelComponent.string = damageStr;

                const progressBarNode = childNode.children[2];
                const progressBarWidth = (250 * GameGlobalVariable.nowwShangHai[key]) / totalDamage;
                progressBarNode.width = progressBarWidth;
            } else if (childNode) {
                childNode.active = false;
            }
        }
    }

    /**
     * 格式化数字为带有中文单位的字符串
     * 建议方法名: formatNumberWithChineseUnit
     * @param num 
     * @returns 
     */
    public formatNumberWithChineseUnit(number: number): string {
        if (number >= 1e8) {
            return (number / 1e8).toFixed(1) + "E";
        } else if (number >= 1e4) {
            return (number / 1e4).toFixed(1) + "W";
        } else {
            return number.toString().split(".")[0];
        }
    }
}

export default ViewAttackTongJi;